import { Component,Injector } from '@angular/core';
import { BasePage } from './../../pages/basePage';
import { StorageService } from './../../providers/StorageService';
import { UserService } from './../../providers/UserService';
import { PIC_URL} from "./../../providers/Config";
import { Events } from 'ionic-angular';
declare var $: any;
/**
 * Generated class for the RefundComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'refund',
  templateUrl: 'refund.html'
})
export class RefundComponent extends BasePage{

  text: string;
  flag:boolean;
  picrooturl="";//图片服务地址
  userinfo={}//获取内存中的数据
  orderlist=[];
  pop=1;    //分页
  bottom1:boolean=false;    //加载全部底部显示器
 
  constructor(injector: Injector,public userService: UserService,public stc: StorageService,public events: Events) {
    super(injector);
    console.log('Hello RefundComponent Component');
    this.picrooturl=PIC_URL
    this.userinfo=this.stc.read('userinfo');  
    this.getMyOrderListAPI('');
    events.subscribe('user:created',(key)=>{
      if(key == 0){
        console.log("全部有数据更新")
        this.again();
      }
    })
  }

  /*获取退款订单*/
  async getMyOrderListAPI(infiniteScroll){
    let param={"user_id":this.userinfo['user_id'],"order_type":3,pop:this.pop};
    let loader = this.loadingCtrl.create({
      content: "",
    });
    let result = await this.userService.getMyOrderListAPI(param);
    loader.dismiss();
    console.log(result);
    if(result.code==1){
      this.pop++;
      this.orderlist = this.orderlist.concat(result.data);
      if(this.orderlist.length == 0){
        this.flag=false;
      }else{
        this.flag=true;
      }
      if(infiniteScroll){
      
        infiniteScroll.complete(); //请求完成数据更新页面
        if(result.data.length<6){
          $(".contain3 ion-infinite-scroll-content").css("display","none");
          this.bottom1=true;
        }
      }
    }else{
      this.flag =false;
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }
  }

  //无限滚动
  doInfinite(infiniteScroll) {
    this.bottom1 = false;
    this.getMyOrderListAPI(infiniteScroll);
  }

  //删除订单
  async deletOrder(i){
    console.log(i)
    let param={order_sn:i.order_sn,user_id:this.userinfo['user_id'],operate_type:3}
    let result= await this.userService.editOrderAPI(param);
    console.log(result);
    if(result.error_code==0){
      let toast = this.toastCtrl.create({
        message: '删除成功！',
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
      this.again();
      let key=0;
      this.events.publish("user:created",key)
    }else{
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
    }
  } 
  //   console.log(this.orderlist);
    // if(result.code ==1){
    //   this.orderlist=result.data;
    //   if(this.orderlist.length==0){
    //     this.flag=true;
    //     console.log(21)
    //   }else{
    //     this.flag=false;
    //   }
    //   console.log(this.orderlist);
    // }else{
    //   this.showAlert(result.msg);
    // }
  //}
  again(){
    this.pop=1;
    this.bottom1 = false;
    this.orderlist=[];
    $(".contain3 ion-infinite-scroll-content").css("display","block");
    this.getMyOrderListAPI('');
  }
}
